package net.oschina.arvin.sqlbuilder;

import net.oschina.arvin.sqlbuilder.definition.*;
import net.oschina.arvin.sqlbuilder.enums.CompareType;
import net.oschina.arvin.sqlbuilder.utils.AssertUtil;
import net.oschina.arvin.sqlbuilder.utils.Util;

import java.util.*;

/**
 * @author Arvin
 * @time 2017/11/17 20:11
 */
public class GeneralSelectBuilder extends AbstractSelectBuilder<GeneralSelectBuilder> {

    /** 查询条件项 */
    private List<ConditionItem> conditionItemList = new ArrayList<ConditionItem>();

    /** Order By 语句 */
    private Set<OrderItem> orderBySet = new HashSet<OrderItem>();

    public GeneralSelectBuilder(Class<?> modelType) {
        super(modelType);
    }

    /**
     * 设置排序字段, 默认是升序
     */
    public GeneralSelectBuilder orderBy(String modelFieldName) {
        return orderBy(0, modelFieldName, true);
    }

    public GeneralSelectBuilder orderBy(String modelFieldName, boolean asc) {
        return orderBy(0, modelFieldName, asc);
    }

    /**
     * 指定顺序
     *
     * @param order          越小则排在越前
     * @param modelFieldName java属性名称
     * @param asc            是否升序
     */
    public GeneralSelectBuilder orderBy(int order, String modelFieldName, boolean asc) {
        this.orderBySet.add(new OrderItem(order, this.getModelType(), modelFieldName, asc));
        return self();
    }

    /**
     * 添加查询条件
     *
     * @param modelFieldName 模型属性名称
     * @param value          值
     * @param compareType    比较类型
     * @return
     */
    public GeneralSelectBuilder addCondition(String modelFieldName, Object value, CompareType compareType) {
        AssertUtil.assertNotBlank(modelFieldName, "要添加的查询条件不能为空");

        ColumnDefinition modelColumnDefinition = getColumnDefinitionByModelFieldName(modelFieldName);

        return addCondition(modelColumnDefinition, value, compareType);

    }

    /**
     * 添加查询条件， 默认是等值查询
     *
     * @param modelFieldName 模型属性名称
     * @param value          值
     * @return
     */
    public GeneralSelectBuilder addCondition(String modelFieldName, Object value) {
        return addCondition(modelFieldName, value, CompareType.EQUAL);
    }

    /**
     * 添加查询条件，模型是等值查询
     *
     * @param columnDefinition 模型定义
     * @param value            值
     * @return
     */
    public GeneralSelectBuilder addCondition(ColumnDefinition columnDefinition, Object value) {
        return addCondition(columnDefinition, value, CompareType.EQUAL);
    }

    /**
     * 添加查询条件， 默认是等值查询
     *
     * @param columnDefinition 模型属性定义
     * @param value            值
     * @param compareType      比较类型， 默认是 等值比较
     * @return
     */
    public GeneralSelectBuilder addCondition(ColumnDefinition columnDefinition, Object value, CompareType compareType) {

        AssertUtil.assertNotNull(columnDefinition, "条件属性列定义不能为null");

        compareType = compareType == null ? CompareType.EQUAL : compareType;

        DirectGeneralConditionItem directGeneralConditionItem = new DirectGeneralConditionItem(columnDefinition, value, compareType);

        conditionItemList.add(directGeneralConditionItem);

        return this;
    }

    /**
     * 添加范围查询条件
     *
     * @param modelFieldName 模型类型名称
     * @param first          第一个值
     * @param second         第二个值
     * @return
     */
    public GeneralSelectBuilder addRangeCondition(String modelFieldName, Object first, Object second) {

        AssertUtil.assertNotBlank(modelFieldName, "要添加的查询条件不能为空");

        return addRangeCondition(getColumnDefinitionByModelFieldName(modelFieldName), first, second);
    }

    /**
     * 添加范围查询条件
     *
     * @param columnDefinition 模型属性定义
     * @param first            第一个值
     * @param second           第二个值
     * @return
     */
    public GeneralSelectBuilder addRangeCondition(ColumnDefinition columnDefinition, Object first, Object second) {

        AssertUtil.assertNotNull(columnDefinition, "要添加的查询条件不能为空");

        RangeGeneralConditionItem rangeGeneralConditionItem = new RangeGeneralConditionItem(columnDefinition, first, second);

        conditionItemList.add(rangeGeneralConditionItem);

        return this;
    }

    /**
     * 解析排序字段列表
     *
     * @return 返回需要排序的字段列表
     */
    @Override
    protected List<OrderItem> parseOrderItemList() {

        List<OrderItem> orderItemList = new ArrayList<OrderItem>(this.orderBySet);

        Collections.sort(orderItemList);

        return orderItemList;
    }

    @Override
    protected GeneralSelectBuilder self() {
        return this;
    }

    @Override
    protected Object getQueryCondition() {
        return null;
    }

    @Override
    protected List<ConditionItem> parseConditionItemList() {
        if (Util.isNotEmpty(this.conditionItemList)) {
            Iterator<ConditionItem> iter = this.conditionItemList.iterator();
            while (iter.hasNext()) {
                ConditionItem conditionItem = iter.next();
                if (isCustomIgnoreCondition(getColumnDefinitionByModelFieldName(conditionItem.getQueryConditionField().getName()))) {
                    iter.remove();
                }
            }
        }
        return this.conditionItemList;
    }
}
